#!/usr/bin/env python
# encoding: utf-8
# @author: liusir
# @file: run_api_cases.py
# @time: 2021/6/27 10:38 上午

import os
import unittest
from common import HTMLTestReportCN
from common import common_function
from common.log_utils import logger

logger.info( '～～～～接口自动化测试开始执行～～～～' )
current_dir = os.path.dirname( os.path.abspath(__file__) )
report_dir = os.path.join( current_dir , 'html_report/' )
case_dir = os.path.join( current_dir , 'testcases' )

#初始化token值
common_function.set_token_to_ini_file()

# 加载用例到套件
discover = unittest.defaultTestLoader.discover( start_dir = case_dir,
                                                pattern="test*.py")
all_cases_suite = unittest.TestSuite()
all_cases_suite.addTest( discover )

# 创建测试报告文件并执行测试
report_dir_obj = HTMLTestReportCN.ReportDirectory(report_dir) # 创建报告对象
report_dir_obj.create_dir( 'API_TEST_' ) # 创建文件夹
report_html_path = HTMLTestReportCN.GlobalMsg.get_value( 'report_path' )
report_html_file_obj = open( report_html_path,'wb' )
logger.info( '～～～～创建测试报告文件成功，开始执行接口用例～～～～' )
html_runner = HTMLTestReportCN.HTMLTestRunner(stream=report_html_file_obj,
                                              title="接口自动化测试报告",
                                              description="微信公众",
                                              tester="P7P8")
html_runner.run( all_cases_suite )
logger.info( '～～～～接口自动化测试执行结束～～～～' )
report_html_file_obj.close()
